//
//  HMFocusTableViewCell.m
//  localLife
//
//  Created by Limonwei on 15/11/26.
//  Copyright © 2015年 heima. All rights reserved.
//

#import "HMFocusTableViewCell.h"
#import "iCarousel.h"
#import "UIImageView+WebCache.h"
#import "HMFocus.h"

@interface HMFocusTableViewCell ()

@property (weak, nonatomic) IBOutlet iCarousel *icarousel;

@property (nonatomic, assign) BOOL wrap;
@property (nonatomic, strong) NSMutableArray *items;

@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UIPageControl *pageControl;

@end
@implementation HMFocusTableViewCell

- (void)awakeFromNib {
    // Initialization code
    
    //设置初始化数据
    [self setUp];
    
    
    
    //设置type
    self.icarousel.type = iCarouselTypeLinear;
    
    
    
    [_icarousel reloadData];
}


-(void)setModel:(HMHomeModel *)model{
    
    _model = model;
    
    _pageControl.numberOfPages = [_model.focus.list count];
    
    [_icarousel reloadData];
    
}

- (id)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        
        //设置初始化数据
        [self setUp];
        
        //设置type
        self.icarousel.type = iCarouselTypeLinear;
        
    }
    
    return self;
}

#pragma mark -- 滚动视图
- (void)setUp
{
    //set up data
    //能不能够循环
    self.wrap = YES;
    
}


- (void)dealloc
{
    _icarousel.delegate = nil;
    _icarousel.dataSource = nil;
    
}

#pragma mark -
#pragma mark View lifecycle

- (BOOL)shouldAutorotateToInterfaceOrientation:(__unused UIInterfaceOrientation)interfaceOrientation
{
    return YES;
}


#pragma mark -
#pragma mark UIActionSheet methods

- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex
{
    if (buttonIndex >= 0)
    {
        //map button index to carousel type
        iCarouselType type = buttonIndex;
        
        //carousel can smoothly animate between types
        [UIView beginAnimations:nil context:nil];
        self.icarousel.type = type;
        [UIView commitAnimations];
        
        
    }
}

#pragma mark -
#pragma mark iCarousel methods

- (NSInteger)numberOfItemsInCarousel:(__unused iCarousel *)carousel
{
    return (NSInteger)[_model.focus.list count];
}

- (UIView *)carousel:(__unused iCarousel *)carousel viewForItemAtIndex:(NSInteger)index reusingView:(UIView *)view
{
    UILabel *label = nil;
    
    //create new view if no view is available for recycling
    if (view == nil)
    {
        
        //获取到 实例
        
        HMFocus *focus = _model.focus.list[index];
        
        //设置图片
        view = [[UIImageView alloc] initWithFrame:self.bounds];
        //        ((UIImageView *)view).image = [UIImage imageNamed:@"123.jpg"];
        
        [((UIImageView *)view) sd_setImageWithURL:[NSURL URLWithString:focus.cover]];
        
        //设置标题
        _titleLabel.text = focus.title;
        
        //修改page的现实
        _pageControl.currentPage = index;
        
        
        view.contentMode = UIViewContentModeScaleToFill;
        
    }
    else
    {
        
    }
    
    
    
    //更改背景颜色
    view.backgroundColor = [UIColor redColor];
    
    return view;
}

- (NSInteger)numberOfPlaceholdersInCarousel:(__unused iCarousel *)carousel
{
    
    return 2;
}

- (UIView *)carousel:(__unused iCarousel *)carousel placeholderViewAtIndex:(NSInteger)index reusingView:(UIView *)view
{
    UILabel *label = nil;
    
    //create new view if no view is available for recycling
    if (view == nil)
    {
        
        //获取到 实例
        
        HMFocus *focus = _model.focus.list[index];
        
        
        view = [[UIImageView alloc] initWithFrame:self.bounds];
        
        [((UIImageView *)view) sd_setImageWithURL:[NSURL URLWithString:focus.cover]];
        
        view.contentMode = UIViewContentModeCenter;
        
        
        //设置标题
        _titleLabel.text = focus.title;
        
        //修改page的现实
        _pageControl.currentPage = index;
        
        
    }
    else
    {
        //get a reference to the label in the recycled view
        //        label = (UILabel *)[view viewWithTag:1];
    }
    
    //更改背景颜色
    view.backgroundColor = [UIColor redColor];
    
    return view;
}

- (CATransform3D)carousel:(__unused iCarousel *)carousel itemTransformForOffset:(CGFloat)offset baseTransform:(CATransform3D)transform
{
    //implement 'flip3D' style carousel
    transform = CATransform3DRotate(transform, M_PI / 8.0f, 0.0f, 1.0f, 0.0f);
    return CATransform3DTranslate(transform, 0.0f, 0.0f, offset * self.icarousel.itemWidth);
}

- (CGFloat)carousel:(__unused iCarousel *)carousel valueForOption:(iCarouselOption)option withDefault:(CGFloat)value
{
    //customize carousel display
    switch (option)
    {
        case iCarouselOptionWrap:
        {
            //normally you would hard-code this to YES or NO
            return self.wrap;
        }
        case iCarouselOptionSpacing:
        {
            //add a bit of spacing between the item views
            return value * 1.0f;
        }
        case iCarouselOptionFadeMax:
        {
            if (self.icarousel.type == iCarouselTypeCustom)
            {
                //set opacity based on distance from camera
                return 0.0f;
            }
            return value;
        }
        case iCarouselOptionShowBackfaces:
        case iCarouselOptionRadius:
        case iCarouselOptionAngle:
        case iCarouselOptionArc:
        case iCarouselOptionTilt:
        case iCarouselOptionCount:
        case iCarouselOptionFadeMin:
        case iCarouselOptionFadeMinAlpha:
        case iCarouselOptionFadeRange:
        case iCarouselOptionOffsetMultiplier:
        case iCarouselOptionVisibleItems:
        {
            return value;
        }
    }
}

#pragma mark -
#pragma mark iCarousel taps

- (void)carousel:(__unused iCarousel *)carousel didSelectItemAtIndex:(NSInteger)index
{
    NSNumber *item = (_model.focus.list)[(NSUInteger)index];
    NSLog(@"Tapped view number: %@", item);
}

- (void)carouselCurrentItemIndexDidChange:(__unused iCarousel *)carousel
{
    NSLog(@"Index: %@", @(self.icarousel.currentItemIndex));
    
    //
    HMFocus *focus = _model.focus.list[self.icarousel.currentItemIndex];
    //设置标题
    _titleLabel.text = focus.title;
    
    //修改page的当前页码
    _pageControl.currentPage = self.icarousel.currentItemIndex;
}

@end
















